import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {

        Map<String, List<String>> map=new HashMap<>();


        for(String str:strs){
            char[] arr=str.toCharArray();
            char[] count=new char[26];
            for(int i=0;i<arr.length;i++){
                count[arr[i]-'a']++;
            }

            StringBuffer key1=new StringBuffer();

            for(int i=0;i<26;i++){
                while(count[i]--!=0) key1.append(i+'a');

            }
            String key=key1.toString();

            List<String> list=map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);

        }

        return new ArrayList<List<String>>(map.values());


    }
}